/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package clusteranalyst2;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author Administrator
 */
public class RefactorHashMap
{
    private double[] generateNewRow(double[] class1,double [] class2,int i,int j)
    {
        double[] a=new double[class1.length-1];
        int kk=0;
        for(int k=0;k<class1.length;k++)
        {
            if(k==i||k==j)
            {
                continue;
            }
            else
            {
                a[kk]=Math.min(class1[k], class2[k]);
                kk++;
            }
        }
        a[kk]=0.00;
        return a;
    }
    //重构矩阵，为分出下一个类准备
    public HashMap excuteRefactor(HashMap oldHashMap)
    {
        //变量
        int colLen;//哈希表行的长度
        double[] class1;//用于生成新类别的第一个类
        double[] class2;//用于生成新类别的第二个类

        //先找到最小距离的值，然后可以从类中获取想要的行列数值
        MinDistance md=new MinDistance();        
        md.findMinDistance(oldHashMap);        
        ////////////////////获取哈希表中第一行的长度///////////////注意：第一行很可能在重构数组的过程中被删除了
        Set keySet=oldHashMap.keySet();
        Iterator it1=keySet.iterator();
        int firstKey=0;
        while(it1.hasNext())
        {
            firstKey=Integer.parseInt(it1.next().toString());
            if(firstKey!=0)
            {
                break;
            }
            else
            {
                System.out.println("************************RefactorHashMap.excuteRefactor()不能得到哈希表的colLen");
            }
        }
        colLen=((double[])(oldHashMap.get(firstKey))).length;
         ///////////////////////生成新的类别//////////////////////////////////////////////////////////////
        class1=new double[colLen];
        class2=new double[colLen];
        class1=(double[])(oldHashMap.get(md.hmRow));
        class2=(double[])(oldHashMap.get(md.hmCol));
        double[] rowGenerated=generateNewRow(class1, class2, md.arrRow, md.arrCol);
        /////////////////删除ij相关的行和列/////////////////////////
        HashMap hmNew=new HashMap(GlobalVar.hmCapacity);
        Set entry=oldHashMap.entrySet();
        Iterator it=entry.iterator();
        int key=0;
        int count=0;
        while(it.hasNext())
        {
            //删除最小值所在的行和列
             Map.Entry me=(Map.Entry)it.next();
             double [] a=new double[colLen];
             a=(double[])(me.getValue());
             key=Integer.parseInt(me.getKey().toString());//得到哈希表的key值
             if(key==md.hmRow||key==md.hmCol)
             {
              //忽略行
                 continue;
             }
            else
            {
                 double[] arrNew=new double[colLen-1];
                 int arrNewIndex=0;
                 for(int i=0;i<a.length;i++)
                 {
                     if(i==md.arrRow||i==md.arrCol)
                     {
                         //忽略列
                         continue;
                     }
                     else
                     {
                         arrNew[arrNewIndex]=a[i];
                         arrNewIndex++;
                     }
                 }
                 //将新生成的行逐一插入到每一行的的末端
                 arrNew[arrNewIndex]=rowGenerated[count];
                 count++;
                 hmNew.put(key, arrNew);
            }
        }
        //将新类加入到哈希表中
        //case1:最后一行被删除了
        System.out.println("+++++++++++++++++hm old size:"+hmNew.size());
        if(hmNew.get(key)==null)
        {
            hmNew.put(key, rowGenerated);
            GlobalVar.currentClassNum++;
            System.out.println("+++++++++++++++++hm new size:"+hmNew.size());
        }
        //case2:最后一行未被删除，将新类加入到末端
        else
        {
           
            hmNew.put(GlobalVar.currentClassNum,rowGenerated);
            GlobalVar.currentClassNum++;
            System.out.println("+++++++++++++++++hm new size:"+hmNew.size()+"  XXXXXnot null");
        }
       PrintMethods pm1=new PrintMethods();       
      //pm1.printHashMap(hmNew);
      return hmNew;
    }

}
